var   fs = require('fs'),
    path = require('path');

module.exports.generateFile = function(controllerName){
    console.log("GENERATED_FILE -- > " + controllerName);
    var target = path.join(__dirname, "..", 'controllers', controllerName + 'controller.js');
    var formView = path.join(__dirname, "..",'views' ,controllerName, controllerName + "-form.ejs");
    var listView = path.join(__dirname, "..",'views' , controllerName, controllerName + "-list.ejs");
    var viewFolder = path.join(__dirname, "..",'views' , controllerName);

    fs.exists(target, function (exists) {
      console.log("DIRECTORY: " + target );
      console.log(exists ? controllerName + " is existing...." : controllerName + " was created");

      if(!exists){
            var controllerContent = 
            'var BaseController = new require("./basecontroller");\n\n' +
            'function Controller(){\n'+
            '  BaseController.call(this);\n'+
            '}\n\n'+
            'Controller.prototype = Object.create (BaseController.prototype);\n'+
            'Controller.prototype.constructor = Controller;\n'+
            'Controller.prototype.FORM_ENTRIES = Array("firstname", "lastname" , "middlename", "username");\n'+
            'Controller.prototype.SEARCH_ENTRIES = Array("firstname", "lastname");\n'+
            'Controller.prototype.TABLE = "'+controllerName+'"; //table for this controller\n'+
            'Controller.prototype.CONTROLLER = "'+controllerName+'"; //controllername\n' + 
            'Controller.prototype.PAGE = Controller.prototype.CONTROLLER;\n' + 
            'Controller.prototype.FORM_VIEW = "'+controllerName+'/' +controllerName+ '-form";\n'+
            'Controller.prototype.LIST_VIEW = "'+controllerName+'/' +controllerName+ '-list";\n\n'+
            'module.exports = new Controller();\n';

            fs.writeFile(target, controllerContent, function (err) {
            if (err) throw err;
                console.log('Controller saved!');
            });
      }
    });


    //fs.mkdir(controllerName, 7777);

    fs.exists(viewFolder, function (viewFolderExists) {
      if(!viewFolderExists){
            fs.mkdir(viewFolder, 7775);
            fs.exists(formView, function (formViewExists) {
               if(!formViewExists){
                    fs.writeFile(formView , "<% layout(\"../../includes/layout-form\") -%>\n FORM HERE", function (err) {
                    if (err) throw err;
                        console.log(controllerName + ' Form View saved!');
                    });
               }
            });
            fs.exists(listView, function (listViewExists) {
               if(!listViewExists){
                    fs.writeFile(listView, "<% layout(\"../../includes/layout-list\") -%>\n LIST HERE", function (err) {
                    if (err) throw err;
                        console.log(controllerName + ' list View saved!');
                    });
                }
            });
      }
    });

}